// pages/attendance/attendance.js
const {
    imgConfig, config
} = require('../../utils/config.js')
const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        imgUrl: `${imgConfig}`,
        statusBarHeight: app.globalData.statusBarHeight,
        late: 0,
        early: 0,
        leave: 0,
        currentMonth: '',
        

    },

    goAttendanceCount(){
        var currentMonth = this.data.currentMonth
        var currentYear = this.data.currentYear
        wx.navigateTo({
          url: `/pages/attendancecount/attendancecount?currentMonth=${currentMonth}&currentYear=${currentYear}`,
        })
    },
    getAttendanceCount(currentMonth,currentYear){
        var that = this
        wx.request({
          url: `${config}/wechat/wxAttendance/getAttendanceList`,
          method:'get',
          data:{
              stuId: app.globalData.userInfo.id,
              currentMonth:currentMonth,
              currentYear:currentYear
          },    
          success(resp){
            console.log(resp.data);
            if(resp.data.countMonth){
              let leaveList =[]
                for (let i = 0; i < resp.data.countMonth.length; i++) {
                if (resp.data.countMonth[i].beginDate !=resp.data.countMonth[i].endDate) {
                  let a = resp.data.countMonth[i]['endDate'].substring(8)-resp.data.countMonth[i]['beginDate'].substring(8)
                  for (let j = 0; j < a; j++) {
                    // 定义时间
                    var diyDate = new Date(resp.data.countMonth[i]['beginDate'])
                    //加1天
                    let date = new Date(diyDate.setDate(diyDate.getDate() + j));
                    // 拼接日期 yyyy-MM-dd 注意月份要 + 1
                    let begendList = date.getFullYear() + "-" + ((date.getMonth() + 1) > 9 ? date.getMonth() + 1 : "0" + (date.getMonth() + 1)) + "-" + (date.getDate() > 9 ? date.getDate() : "0" + date.getDate());
                    leaveList.push(begendList)
                  }
                }else{
                  leaveList.push(resp.data.countMonth[i]['applyDate'])
                }
              }  
              that.setData({
                leave:leaveList.length
            })
            }
             
          }
        })
    },

    getAttendDateList(currentMonth,currentYear){
        var that = this 
        wx.request({
          url: `${config}/wechat/wxAttendance/stuDateList`,
          method:'get',
          data:{
              stuId: app.globalData.userInfo.id,
              currentYear:currentYear,
              currentMonth:currentMonth
          },
          success(resp){
              console.log(resp)
              that.setData({
                  clockData:resp.data.stuDateList.attendDate,
                  late:resp.data.stuDateList.lateDate.length,
                  early:resp.data.stuDateList.earlyDate.length
              })
          }
        })
    },
    getAttendDateList(currentMonth,currentYear){
      var that = this 
      wx.request({
        url: `${config}/wechat/wxclock/stuAttendList`,
        method:'get',
        data:{
            stuId: app.globalData.userInfo['id'],
            currentYear:currentYear,
            currentMonth:currentMonth
        },
        success(resp){
            that.setData({
              clockData:resp.data.stuAttendList.stuAttendList,
              clockCount:resp.data.stuAttendList.stuAttendList.length
            })
        }
      })
  },
    changeMonth(e) {
        console.log("日历组件月改变事件", e);
        var currentMonth = e.detail.month
        var currentYear = e.detail.year
        console.log(currentMonth,currentYear)
        this.setData({
            currentMonth: currentMonth,
            currentYear:currentYear,
        })
        this.getAttendanceCount(currentMonth,currentYear)
        this.getAttendDateList(currentMonth,currentYear)
    },
    back: function () {
        wx.navigateBack({});
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        let dateObject = new Date()
        var currentMonth = dateObject.getMonth() + 1
        var currentYear = dateObject.getFullYear()
        this.setData({
            currentMonth: currentMonth,
            currentYear:currentYear
        })
        this.getAttendDateList(currentMonth,currentYear)
        this.getAttendanceCount(currentMonth,currentYear)
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {
       
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
       
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})